home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / cxref.el < prev    next >
Lisp/Scheme  |  1996-11-16  |  6KB  |  198 lines

  1. ;; $Header: /home/amb/cxref/RCS/cxref.el 1.5 1996/11/16 15:29:51 amb Exp $
  2. ;;
  3. ;; C Cross Referencing & Documentation tool. Version 1.3.
  4. ;;
  5. ;; Some useful Emacs lisp functions for use with cxref.
  6. ;; Adds in comments of the appropriate format for cxref.
  7. ;;
  8. ;; Written by Andrew M. Bishop
  9. ;;
  10. ;; This file Copyright 1995,96 Andrew M. Bishop
  11. ;; It may be distributed under the GNU Public License, version 2, or
  12. ;; any higher version.  See section COPYING of the GNU Public license
  13. ;; for conditions under which this file may be redistributed.
  14. ;;
  15.  
  16. ;;;###autoload
  17. (defun cxref-c-mode-common-hook () "Set up the key bindings for cxref in cc-mode"
  18.   (define-key c-mode-map "\C-c\C-f"  'cxref-file-comment)     ;; Control-C Control-F
  19.   (define-key c-mode-map "\C-cf"     'cxref-function-comment) ;; Control-C f
  20.   (define-key c-mode-map "\C-cv"     'cxref-variable-comment) ;; Control-C v
  21.   (define-key c-mode-map "\C-ce"     'cxref-endline-comment)  ;; Control-C e
  22.   (define-key c-mode-map "\C-ci"     'cxref-inline-comment)   ;; Control-C i
  23. )
  24.  
  25. ;;;###autoload
  26. (add-hook 'c-mode-common-hook 'cxref-c-mode-common-hook)
  27.  
  28. ;; Insert a file comment suitable for parsing with cxref
  29.  
  30. (defun cxref-file-comment () "Inserts a file comment suitable for parsing with cxref" (interactive)
  31.   (let ((cp (make-marker)))
  32.  
  33.     (goto-char (point-min))
  34.  
  35.     (insert   "/***************************************") (c-indent-command)
  36.     (insert (concat "\n$" "Header" "$")) (c-indent-command)
  37.     (insert "\n")
  38.     (insert "\n")(c-indent-command)
  39.     (set-marker cp (point))
  40.     (insert "\n***************************************/") (c-indent-command)
  41.     (insert "\n\n\n")
  42.     (while (looking-at "\n") (delete-char 1))
  43.  
  44.     (if (string-match "\\.h$" (buffer-file-name))
  45.         (let ((st) (defname (file-name-nondirectory (buffer-file-name))))
  46.           (while (setq st (string-match "\\." defname))
  47.             (setq defname (concat (substring defname 0 st) "_" (substring defname (+ 1 st) nil))))
  48.           (setq defname (upcase defname))
  49.           (insert (concat "#ifndef " defname "\n"))
  50.           (insert (concat "#define " defname "    /*+ To stop multiple inclusions. +*/\n"))
  51.           (insert "\n")
  52.           (goto-char (point-max))
  53.           (while (looking-at-backward "\n") (delete-char -1))
  54.           (insert "\n\n")
  55.           (insert (concat "#endif /* " defname " */\n"))
  56.           ))
  57.  
  58.     (goto-char cp) (c-indent-command)
  59.     ))
  60.  
  61. ;; Insert a function comment suitable for parsing with cxref
  62.  
  63. (defun cxref-function-comment () "Inserts a function comment suitable for parsing with cxref" (interactive)
  64.   (let ((bp (make-marker)) (cp (make-marker)) (fp (make-marker)) (as) (ae) (a) (depth 0))
  65.  
  66.     (beginning-of-line)
  67.  
  68.     (while (looking-at-backward "\n\n") (delete-backward-char 1))
  69.  
  70.     (insert "\n\n")
  71.     (insert "/*++++++++++++++++++++++++++++++++++++++") (c-indent-command)
  72.     (insert "\n")
  73.     (set-marker bp (point))
  74.     (insert "\n")
  75.     (set-marker cp (point))
  76.     (insert "++++++++++++++++++++++++++++++++++++++*/") (c-indent-command)
  77.     (insert "\n\n")
  78.  
  79.     (if (looking-at "static") (re-search-forward "[ \t\n]+"))
  80.     (set-marker fp (point))
  81.  
  82.     (if (not (looking-at "void[\t ]+[a-zA-Z0-9_]"))
  83.         (progn
  84.           (setq as (point))
  85.           (search-forward "(") (backward-char)
  86.           (setq ae (point))
  87.           (setq a (buffer-substring as ae))
  88.           (goto-char cp)
  89.           (insert "\n")
  90.           (insert a) (c-indent-line)
  91.           (insert "\n")
  92.           (set-marker cp (point))
  93.           (goto-char fp)
  94.           )
  95.       )
  96.  
  97.     (search-forward "(") (set-marker fp (point))
  98.  
  99.     (catch 'finished-args
  100.  
  101.       (while t
  102.  
  103.         (if (looking-at-backward ")")
  104.             (throw 'finished-args t))
  105.  
  106.         (re-search-forward "[\n\t ]*")
  107.         (set-marker fp (point))
  108.         (setq as (point))
  109.  
  110.         (while (not (and (= depth 0) (looking-at "[,)]")))
  111.           (if (looking-at "[({]")
  112.               (setq depth (1+ depth)))
  113.           (if (looking-at "[)}]")
  114.               (setq depth (1- depth)))
  115.           (forward-char))
  116.  
  117.         (set-marker fp (1+ (point)))
  118.  
  119.         (re-search-backward "[\n\t ]*")
  120.         (setq ae (point))
  121.  
  122.         (setq a (buffer-substring as ae))
  123.  
  124.         (if (not (or (string= a "void") (string= a "")))
  125.             (progn
  126.               (goto-char cp)
  127.               (insert "\n")
  128.               (insert a) (c-indent-line)
  129.               (insert "\n")
  130.               (set-marker cp (point))
  131.               ))
  132.         (goto-char fp)
  133.         ))
  134.  
  135.     (goto-char bp) (c-indent-command)
  136.     ))
  137.  
  138. ;; Insert a variable comment suitable for parsing with cxref
  139.  
  140. (defun cxref-variable-comment () "Inserts a variable comment suitable for parsing with cxref" (interactive)
  141.   (let ((fp (make-marker)))
  142.     (beginning-of-line)
  143.  
  144.     (while (looking-at-backward "\n\n") (delete-backward-char 1))
  145.  
  146.     (insert "\n")
  147.     (insert "/*+ ")
  148.     (set-marker fp (point))
  149.     (insert " +*/")
  150.     (insert "\n")
  151.  
  152.     (goto-char fp) (c-indent-command)
  153.     ))
  154.  
  155. ;; Inserts an end of line comment that is parsed by cxref
  156.  
  157. (defun cxref-endline-comment () "Inserts an end of line comment that is parsed by cxref" (interactive)
  158.   (let ((fp (make-marker)))
  159.     (end-of-line)
  160.     (indent-to-column (c-comment-indent))
  161.  
  162.     (insert "/*+ ")
  163.     (setq fp (point))
  164.     (insert " +*/")
  165.  
  166.     (goto-char fp)
  167.     ))
  168.  
  169. ;; Insert an inline comment that is not parsed with cxref
  170.  
  171. (defun cxref-inline-comment () "Inserts an inline comment that is not parsed with cxref" (interactive)
  172.   (let ((fp (make-marker)))
  173.     (beginning-of-line)
  174.  
  175.     (while (looking-at-backward "\n\n") (delete-backward-char 1))
  176.  
  177.     (insert "\n")
  178.     (insert "/* ")
  179.     (set-marker fp (point))
  180.     (insert " */")
  181.     (insert "\n\n")
  182.  
  183.     (goto-char fp) (c-indent-command)
  184.     ))
  185.  
  186. ;;  A Very Useful Function
  187.  
  188. (defun looking-at-backward (arg)
  189.   (save-excursion
  190.     (let ((cp (point)) (return))
  191.       (if (re-search-backward arg (point-min) t)
  192.           (if (re-search-forward arg cp t)
  193.               (if (= (point) cp)
  194.                   (setq return t)
  195.                 )))
  196.       return
  197.       )))
  198.